
package com.duolebo.uteped_sdk.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SPHelper {

	private static final String TAG = "SPHelper";

	private Editor mEditor;
	private final SharedPreferences mPres;

	private static SPHelper defaultInstance;

	public synchronized static SPHelper getDefaultInstance(Context ctx, String name) {
		if (defaultInstance == null) {
			defaultInstance = new SPHelper(ctx, name);
		}
		return defaultInstance;
	}

    public synchronized static SPHelper shareInstance() {
        return defaultInstance;
    }

	public SPHelper(Context c, String preferenceName) {
		mPres = c.getSharedPreferences(preferenceName, 0);
	}

	public boolean putAndCommit(String key, String value) {
		mEditor = mPres.edit();
		mEditor.putString(key, value);
		return commit();
	}

	public boolean putAndCommit(String key, boolean value) {
		mEditor = mPres.edit();
		mEditor.putBoolean(key, value);
		return commit();
	}

	public boolean putAndCommit(String key, int value) {
		mEditor = mPres.edit();
		mEditor.putInt(key, value);
		return commit();
	}

	public boolean putAndCommit(String key, long value) {
		mEditor = mPres.edit();
		mEditor.putLong(key, value);
		return commit();
	}

	public boolean getBoolean(String key, boolean defValue) {
		return mPres.getBoolean(key, defValue);
	}

	public long getLong(String key, long defValue) {
		return mPres.getLong(key, defValue);
	}

	public int getInt(String key, int defValue) {
		return mPres.getInt(key, defValue);
	}

	public void put(String key, String value) {
		mEditor = mPres.edit();
		mEditor.putString(key, value);
	}

	public String getString(String key, String defaultValue) {
		return mPres.getString(key, defaultValue);
	}
	public String getString(String key) {
		return mPres.getString(key, "");
	}

	public boolean commit() {
		return mEditor.commit();
	}

	public void removeAll() {
		mEditor = mPres.edit();
		mEditor.clear();
		commit();
	}

	public void removeAndCommit(String key) {
		mEditor = mPres.edit();
		mEditor.remove(key);
		mEditor.apply();
	}

	public void remove(String key) {
		mEditor = mPres.edit();
		mEditor.remove(key);
	}

}
